import datetime,multiprocessing,os,json,sys
import pandas as pd
import numpy as np

def map_err(r, cnt, err_code, file_handle):
	out = ";".join([
		str(cnt),
		str(err_code),
		str(r.get('MSISDN')),
		str(r.get('CARDPROVIDER')),
		str(r.get('CARDNAME')),
		str(r.get('CARDNUMBER'))
	]) + "\n"
	file_handle.write(out)

def map_ok(r, sub_tmp, cnt, ok_file_handle, err_file_handle):
	for sub_sub_tmp in sub_tmp:
		len_sub_sub_tmp = len(sub_sub_tmp)
		sub_cnt = 0
		for ext in sub_sub_tmp:
			if str(sub_sub_tmp.get(ext)).isdigit():
				#print(sub_sub_tmp.get(ext))
				out = "|".join([
					str(r.get('MSISDN')),
					str(r.get('CARDPROVIDER')),
					str(sub_sub_tmp.get(ext))
				]) + "\n"
				ok_file_handle.write(out)
			else:
				sub_cnt += 1
		if sub_cnt == len_sub_sub_tmp:
			map_err(r, cnt, 'E-005', err_file_handle)

def main():
	input_path = os.path.join(os.getcwd(), sys.argv[1])
	output_ok = os.path.join(os.getcwd(), sys.argv[2])
	output_err = os.path.join(os.getcwd(), sys.argv[3])
	isuniq = sys.argv[4]

	print(f"input_path: {input_path}")
	print(f"output_ok: {output_ok}")
	print(f"output_err: {output_err}")
	print(f"isuniq: {isuniq}")

	output_ok_handle = open(output_ok, 'w', encoding='utf-8')
	output_err_handle = open(output_err, 'w', encoding='utf-8')
	
	try:
		input_dict = pd.read_csv(input_path, dtype=str, sep='|', names=['MSISDN','CARDPROVIDER','CARDNAME','CARDNUMBER'], skiprows=1).to_dict(orient='records')
	except Exception as err:
		print(f"pd.read_csv err: {err}")

	cnt = 1

	for r in input_dict:
		if not pd.isna(r.get('CARDNUMBER')):
			try:
				tmp = json.loads(r['CARDNUMBER'])
				if isinstance(tmp, dict):
					if 'data' in tmp:
						sub_tmp = tmp.get('data')
						if isinstance(sub_tmp, list):
							map_ok(r, sub_tmp, cnt, output_ok_handle, output_err_handle)
						else:
							map_err(r, cnt, 'E-004', output_err_handle)
					else:
						map_err(r, cnt, 'E-003', output_err_handle)
				elif isinstance(tmp, list):
					sub_tmp = tmp
					map_ok(r, sub_tmp, cnt, output_ok_handle, output_err_handle)
				else:
					map_err(r, cnt, 'E-002', output_err_handle)
			except Exception as err:
				print(f"json.loads err: {err} {r}")
		else:
			map_err(r, cnt, 'E-001', output_err_handle)

		cnt += 1

	output_ok_handle.close()
	output_err_handle.close()

	if str(isuniq) in ['Y', 'YES', 'y', 'yes', '1']: 
		cmd = f"sort {output_ok} | uniq > {str(sys.argv[2]).split('.')[0]}_uniq.csv"
		print(f"cmd is {cmd}")
		os.system(cmd)

if __name__ == '__main__':
	time1 = datetime.datetime.now()
	main()
	time2 = datetime.datetime.now()

	print('start time: ', time1)
	print('end time: ', time2)
	print('total time: ', time2 - time1)